package concurrency;

/**
 * @Auther DRevelation
 * @Date 2020-06-10 下午 13:42
 * @Desc
 */
public class ThreadLocalDemo {

    static ThreadLocal<String> localVar = new ThreadLocal<>();

    static void print(String str) {
        //打印当前线程本地内存中localVar变量
        System.out.println(str + ":" + localVar.get());
        //清除当前线程本地内存中的localVar变量
        localVar.remove();
    }

    public static void main(String[] args) {
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                localVar.set("threadone local variable");
                print("threadOne");
                System.out.println("thread remove after: " + localVar.get());
            }
        });

        Thread threadTwo = new Thread(new Runnable() {
            @Override
            public void run() {
                localVar.set("threadtwo local variable");
                print("threadTwo");
                System.out.println("thread remove after: " + localVar.get());
            }
        });

        threadOne.start();
        threadTwo.start();
    }
}
